package dev.alm.aidemo.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("ai")
@CrossOrigin
public class AiController {

    private final ChatClient chatClient;

    public AiController(ChatClient.Builder builder) {
        this.chatClient = builder.defaultSystem("你是一个智能AI助手，请用{voice}的语气回答问题").build();
    }
    @GetMapping("chat")
    public String chat(@RequestParam("message") String message) {
        String content = this.chatClient.prompt().user(message).call().content();
        return content;
    }

    @GetMapping("voice")
    public String voice(@RequestParam("message") String message, @RequestParam("voice") String voice) {
        return this.chatClient.prompt().system(sp -> sp.param("voice", voice)).user(message).call().content();
    }

    @GetMapping(value = "stream", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> stream(@RequestParam("message") String message) {
        return this.chatClient.prompt().user(message).stream().content();
    }

    // 用record定义实体类
    record ActorMovies(String actor, List<String> movies) {}

    @GetMapping("movies")
    public ActorMovies movies(@RequestParam("actor") String actor) {
        return this.chatClient.prompt().user(actor).call().entity(ActorMovies.class);
    }

}
